package com.lg.wechat.common.base;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.lg.utils.RedisTemplateUtil;
import com.lg.utils.StringUtils;
import com.lg.wechat.business.wxuser.entity.WxUser;
import com.lg.wechat.business.wxuser.service.WxUserService;
import com.lg.wechat.utils.Constant;
import com.lg.wechat.utils.CookieUtil;

@Transactional
public  class BaseController{
	
	@Autowired
	WxUserService wxUserService;
	
	@Autowired
	HttpServletRequest request;
	
	public WxUser getUser(){
		Cookie cookie=CookieUtil.getCookieByName(request, Constant.Token);
		if(cookie!=null&&StringUtils.isNotEmpty(cookie.getValue())){
			WxUser wxUser=(WxUser) RedisTemplateUtil.getObj(cookie.getValue());
			if(wxUser==null){
				wxUser=wxUserService.selectByOpenId(cookie.getValue());
			}
			return wxUser;
		}else{
			return null;
		}
	}
	
	public Long getUserId(){
		return getUser().getId();
	}
	
}